#!/bin/bash

# params
cargo_build_thread=1

# install third dep
yum install -y git gcc clang openssl-devel tpm2-tss-devel rpm-build rpmdevtools

# install rust
if command -v rustc &>/dev/null; then
    	echo "✅ Rust 已安装，版本: $(rustc --version)"
else
    	export RUSTUP_UPDATE_ROOT=https://mirrors.aliyun.com/rustup/rustup
    	export RUSTUP_DIST_SERVER=https://mirrors.aliyun.com/rustup
	curl --proto '=https' --tlsv1.2 -sSf https://mirrors.aliyun.com/repo/rust/rustup-init.sh | sh -s -- -y
	source "$HOME/.cargo/env"
fi

# clone code
if [ -d "global-trust-authority" ]; then
	echo "GTA is cloned"
else
	git clone https://gitee.com/openeuler/global-trust-authority.git
fi


# install librdkafka
if pkg-config --exists rdkafka; then
    	echo "✅ librdkafka 已安装"
else
   	git clone --depth 1 --branch v2.10.0 https://gitee.com/mirrors/librdkafka.git 
	cd librdkafka
    	./configure --prefix=/usr --libdir=/usr/lib/x86_64-linux-gnu
	make -j$(proc)
    	make install
    	ldconfig
	export PKG_CONFIG_PATH=/usr/lib/x86_64-linux-gnu/pkgconfig/:$PKG_CONFIG_PATH
	echo "export PKG_CONFIG_PATH=/usr/lib/x86_64-linux-gnu/pkgconfig/:$PKG_CONFIG_PATH" > ~/.bashrc
	cd ..
fi

cd global-trust-authority

# build
# CARGO_BUILD_JOBS=$(cargo_build_thread) cargo build

# host
bash key_manager/script/rpm_build.sh 

bash rpm/script/rpm_build.sh -a -s -c
